package com.stylefeng.guns.modular.system.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.stylefeng.guns.core.model.SysFunTree;
import com.stylefeng.guns.modular.system.model.Function;
import com.stylefeng.guns.modular.system.model.OrgcategoryFunction;
import com.stylefeng.guns.modular.system.dao.OrgcategoryFunctionMapper;
import com.stylefeng.guns.modular.system.service.IFunctionService;
import com.stylefeng.guns.modular.system.service.IOrgcategoryFunctionService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 机构分类功能权限表 服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2018-09-12
 */
@Service
public class OrgcategoryFunctionServiceImpl extends ServiceImpl<OrgcategoryFunctionMapper, OrgcategoryFunction> implements IOrgcategoryFunctionService {
    @Resource
    private OrgcategoryFunctionMapper orgcategoryFunctionMapper;
    @Autowired
    private IFunctionService functionService;
    @Override
    public List<SysFunTree> selectListByCate(String soCate) throws DataAccessException {

        Function fun = new Function();
        fun.setSfState(1);

        //1.获取所有功能树列表
        List<Function> funList = functionService.selectListFunction(fun);

        //2.获取机构分类下已经分配的功能列表
        List<OrgcategoryFunction> orgFunList = this.selectListOrgcategoryFunction(soCate);
        //3.结合1和2构造前台显示的功能树
        List<SysFunTree> listSysFunTree = new ArrayList<SysFunTree>();
        //4.1构造前前台所属机构分类显示的功能树
        for (Function sysFunction:funList) {
            SysFunTree sysFunTree = new SysFunTree();
            listSysFunTree.add(sysFunTree);
            sysFunTree.setPid(sysFunction.getSfPid().toString());
            sysFunTree.setId(sysFunction.getSfId().toString());
            sysFunTree.setName(sysFunction.getSfName());
            if(sysFunction.getSfIsmenu().equals("1")){
                sysFunTree.setFunType("F");//tree节点表示功能
            }else{
                sysFunTree.setFunType("M");//tree节点表示功能
            }
            sysFunTree.setUrl(sysFunction.getSfUrl());
            sysFunTree.setChecked("false");//tree节点是否选中
            if (orgFunList == null) {
                continue;
            }
            for (OrgcategoryFunction tempFun:orgFunList) {
                //tempFun.getSpFunctionId() 表示授权的功能
                //tempFun.getSpMethod() 对当前功能授权的操作 格式:[操作id]-[操作id]-[操作id]
                if (tempFun.getSpFunctionId().toString().equals(sysFunTree.getId()) || tempFun.getSpMethod().indexOf("["+sysFunTree.getId()+"]")>-1) {
                    sysFunTree.setChecked("true");
                    break;
                }
            }
        }
        funList.clear();
        orgFunList.clear();
        return listSysFunTree;
    }

    @Override
    @Transactional
    public void insertOrgcategoryFunction(OrgcategoryFunction orgcategoryFunction) throws DataAccessException {

        // [{fid:功能id,sm:[操作id]-[操作id],scid:机构分类id}]
        String savaTxt=orgcategoryFunction.getObjArrys();
        //删除老的机构分类所拥有的功能和操作
        orgcategoryFunctionMapper.deleteOrgcategoryFunction(orgcategoryFunction.getSocId());
        //保存机构分类所拥有的功能和操作
        JSONArray arryOBj=null;
            try{
                arryOBj=JSONArray.parseArray(java.net.URLDecoder.decode(savaTxt,"urf-8"));
            }catch (UnsupportedEncodingException e){
                e.printStackTrace();
            }
        for(int i=0;i<arryOBj.size();i++){
            JSONObject obj=arryOBj.getJSONObject(i);
            OrgcategoryFunction tempOrgcategoryFunction=new OrgcategoryFunction();
            tempOrgcategoryFunction.setSocId(obj.getString("socid"));
            tempOrgcategoryFunction.setSpFunctionId(Integer.parseInt(obj.getString("fid")));
            tempOrgcategoryFunction.setSpMethod(obj.getString("sm"));
            orgcategoryFunctionMapper.insertOrgcategoryFunction(tempOrgcategoryFunction);
        }
    }
    /**
     * 根据机构分类查询所拥有的功能和操作
     * @param soCate 系统操作对象
     * @return
     * @Date 2018-10-02
     */
    public List<OrgcategoryFunction> selectListOrgcategoryFunction(String soCate)throws DataAccessException{
        OrgcategoryFunction orgcategoryFunction=new OrgcategoryFunction();
        orgcategoryFunction.setSocId(soCate);
        return orgcategoryFunctionMapper.selectListOrgcategoryFunction(orgcategoryFunction);
    }
}
